//
//  ViewController.m
//  Livetest
//
//  Created by l t on 2020/5/18.
//  Copyright © 2020 l t. All rights reserved.
//
#define UIColorFromRGBA(rgbValue, alphaValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:alphaValue]

#import "ViewController.h"
#import "Testview.h"
#define SCREENWIDth  [UIScreen mainScreen].bounds.size.width
#define SCREENHEITHG [UIScreen mainScreen].bounds.size.height
#import "YDScorllview.h"
#import "shradMode.h"
#import "Mode.h"
#define PAIG 5

@interface ViewController ()<UITextViewDelegate,UIScrollViewDelegate>
{
    CALayer *_layer;
    int _index;
    NSMutableArray *_images;
    NSDate* tmpStartData;
    NSDate *enddata;
    UILabel *l;
//    CGPoint point=scrollView.contentOffset;

}
@property (nonatomic, strong) UIImageView *ballView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSTimeInterval duration;
@property (nonatomic, assign) NSTimeInterval timeOffset;
@property (nonatomic, strong) id fromValue;
@property (nonatomic, strong) id toValue;
@property (nonatomic,strong) UIScrollView *scolview;

@property (nonatomic,strong) UIView *TopScrollview;
@end

@implementation ViewController
- (void)CreatButton {
    for (int i = 0; i< PAIG; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.font = [UIFont systemFontOfSize:14];
        button.frame = CGRectMake(SCREENWIDth/PAIG*i, 0, SCREENWIDth/PAIG, 40);
        switch (i) {
            case 0:
                [button setTitle:@"温度" forState:UIControlStateNormal];
                [button setTitleColor:UIColorFromRGBA(0x157efb, 1) forState:UIControlStateNormal];
                break;
            case 1:
                [button setTitle:@"轨迹" forState:UIControlStateNormal];
                [button setTitleColor:UIColorFromRGBA(0x36474f,1) forState:UIControlStateNormal];
                break;
            case 2:
                [button setTitle:@"实时" forState:UIControlStateNormal];
                [button setTitleColor:UIColorFromRGBA(0x36474f,1) forState:UIControlStateNormal];
                break;
           case 3:
                [button setTitle:@"震动" forState:UIControlStateNormal];
                [button setTitleColor:UIColorFromRGBA(0x36474f,1) forState:UIControlStateNormal];
                break;
                 default:
                [button setTitle:@"电量" forState:UIControlStateNormal];
                [button setTitleColor:UIColorFromRGBA(0x36474f,1) forState:UIControlStateNormal];
        }
        
        button.tag = 1000+i;
        [button addTarget:self action:@selector(Buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
}
- (void)Buttonclicked:(UIButton *)but{
    
    for (int i =0; i<PAIG; i++) {
        UIButton *button =  [self.view viewWithTag:i+1000];

        if (button.tag == but.tag) {
            [but setTitleColor:UIColorFromRGBA(0x157efb, 1) forState:UIControlStateNormal];
        }else{
            [button setTitleColor:UIColorFromRGBA(0x36474f,1) forState:UIControlStateNormal];
        }
        
    }
    
    [self.scolview setContentOffset:CGPointMake((but.tag-1000)*SCREENWIDth, 0) animated:YES];
    [UIView animateWithDuration:0.5 animations:^{
       
        self.TopScrollview.frame = CGRectMake((but.tag-1000)*SCREENWIDth/PAIG, 40, SCREENWIDth/PAIG, 3);
       

    }];
    
   
    
}
     
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self CreatButton];
    _TopScrollview = [[UIView alloc] initWithFrame:CGRectMake(0, 39, SCREENWIDth/3, 3)];
    _TopScrollview.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_TopScrollview];
    self.scolview   = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 46, SCREENWIDth, 200)];
    self.scolview .delegate = self;
    self.scolview .contentSize = CGSizeMake(SCREENWIDth*3, 200);
    [ self.scolview  setContentOffset:CGPointMake(0, 0) animated:YES];
    self.scolview .pagingEnabled = YES;
    [self.view addSubview: self.scolview ];
    YDScorllview *vc = [[YDScorllview alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDth, 200) WithAnd:0];
    
    YDScorllview *vc1 = [[YDScorllview alloc] initWithFrame:CGRectMake(SCREENWIDth, 0, SCREENWIDth, 200) WithAnd:1];
    
    YDScorllview *vc2 = [[YDScorllview alloc] initWithFrame:CGRectMake(SCREENWIDth*2, 0, SCREENWIDth, 200) WithAnd:2];
    
    YDScorllview *vc3 = [[YDScorllview alloc] initWithFrame:CGRectMake(SCREENWIDth*3, 0, SCREENWIDth, 200) WithAnd:2];
    YDScorllview *vc4 = [[YDScorllview alloc] initWithFrame:CGRectMake(SCREENWIDth*4, 0, SCREENWIDth, 200) WithAnd:2];

    [self.scolview addSubview:vc];
    [self.scolview addSubview:vc1];
    [self.scolview addSubview:vc2];
    [self.scolview addSubview:vc3];
    [self.scolview addSubview:vc4];

    

//    //获取开始时间
//    tmpStartData = [NSDate date];
//       /*( 执行代码段 )*/
//
//       //计算代码段所用时间
//    l = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    [self.view addSubview:l];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
//    CGPoint point=scrollView.contentOffset;
//    NSLog(@"%f",point.x);
}
     - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
         CGPoint point=scrollView.contentOffset;
         if (point.x == SCREENWIDth ||point.x == SCREENWIDth*2) {
             scrollView.scrollEnabled = NO;
         }
         if (point.y < 0 || point.y>0) {
             
             return;
         }else{
            
        
         CGFloat w =  point.x;
             if (scrollView.frame.origin.x != 0) {
                 return;
             }else{
            [UIView animateWithDuration:0.2 animations:^{
                     self.TopScrollview.frame = CGRectMake(point.x /PAIG, 39, SCREENWIDth/PAIG, 3);
              }];
         for (int i = 0; i<PAIG; i++) {
             UIButton *button =  [self.view viewWithTag:i+1000];
             
             if (w/SCREENWIDth == i) {
                 [button setTitleColor:UIColorFromRGBA(0x157efb, 1) forState:UIControlStateNormal];
                 
             }else{
                 [button setTitleColor:UIColorFromRGBA(0x36474f,1) forState:UIControlStateNormal];
                 
             }
         }
       }
     }
     }
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    enddata = [NSDate date];
//    NSLog(@"%ld",[self intervalSecondsWithSmallDate:tmpStartData bigDate:enddata]);
//    __block NSInteger yi = [self intervalSecondsWithSmallDate:tmpStartData bigDate:enddata];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//   [dateFormatter setDateFormat:@"HH:mm:ss"];
//
//
//
//    NSString *strDate = [dateFormatter stringFromDate:tmpStartData];
//    NSLog(@"%@",strDate);
//    NSTimer *t = [NSTimer timerWithTimeInterval:1 repeats:true block:^(NSTimer * _Nonnull timer) {
//        yi--;
//        if (yi==0) {
//            return;
//        }
//        tmpStartData= [NSDate dateWithTimeInterval:yi sinceDate:tmpStartData];
//
//        l.text =[dateFormatter stringFromDate:tmpStartData];
//    //                NSLog(@"%f",timer.timeInterval);
//
//        }];
//        [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes] ;
//
//}
- (NSInteger)intervalSecondsWithSmallDate:(NSDate*)smallDate bigDate:(NSDate*)bigDate

{

    NSCalendar *calendar =[NSCalendar currentCalendar];

    

    

    unsigned int unitFlags =kCFCalendarUnitYear|kCFCalendarUnitMonth|kCFCalendarUnitDay|kCFCalendarUnitHour|kCFCalendarUnitMinute|kCFCalendarUnitSecond;

    

    NSDateComponents *dateComp= [calendar components:unitFlags fromDate:smallDate toDate:bigDate options:0];

    NSInteger second = [dateComp hour]*3600+[dateComp minute]*60+[dateComp second];

    

    return second;

    

}
//-(void)step{
//    //定义一个变量记录执行次数
//    static int s=0;
//    //每秒执行6次
//    if (++s%10==0) {
//        UIImage *image=_images[_index];
//        _layer.contents=(id)image.CGImage;//更新图片
//        _index=(_index+1)%IMAGE_COUNT;
//    }
//}

float interpolate(float from, float to, float time)
{
    return (to - from) * time + from;
}

- (id)interpolateFromValue:(id)fromValue toValue:(id)toValue time:(float)time
{
    if ([fromValue isKindOfClass:[NSValue class]]) {
        //get type
        const char *type = [(NSValue *)fromValue objCType];
        if (strcmp(type, @encode(CGPoint)) == 0) {
            CGPoint from = [fromValue CGPointValue];
            CGPoint to = [toValue CGPointValue];
            CGPoint result = CGPointMake(interpolate(from.x, to.x, time), interpolate(from.y, to.y, time));
            return [NSValue valueWithCGPoint:result];
        }
    }
    //provide safe default implementation
    return (time < 0.5)? fromValue: toValue;
}

float bounceEaseOut(float t)
{
    if (t < 4/11.0) {
        return (121 * t * t)/16.0;
    } else if (t < 8/11.0) {
        return (363/40.0 * t * t) - (99/10.0 * t) + 17/5.0;
    } else if (t < 9/10.0) {
        return (4356/361.0 * t * t) - (35442/1805.0 * t) + 16061/1805.0;
    }
    return (54/5.0 * t * t) - (513/25.0 * t) + 268/25.0;
}

- (void)animate
{
    //reset ball to top of screen
    self.ballView.center = CGPointMake(150, 32);
    //configure the animation
    self.duration = 1.0;
    self.timeOffset = 0.0;
    self.fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 32)];
    self.toValue = [NSValue valueWithCGPoint:CGPointMake(150, 268)];
    //stop the timer if it's already running
    [self.timer invalidate];
    //start the timer
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1/60.0
                                                  target:self
                                                selector:@selector(step:)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)step:(NSTimer *)step
{
    //update time offset
    self.timeOffset = MIN(self.timeOffset + 1/60.0, self.duration);
    //get normalized time offset (in range 0 - 1)
    float time = self.timeOffset / self.duration;
    //apply easing
    time = bounceEaseOut(time);
    //interpolate position
    id position = [self interpolateFromValue:self.fromValue
                                     toValue:self.toValue
                                  time:time];
    //move ball view to new position
    self.ballView.center = [position CGPointValue];
    //stop the timer if we've reached the end of the animation
    if (self.timeOffset >= self.duration) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    //    NSLog(@"%@",[NSStringFromClass(m class)]);
//    Mode *m = [[Mode alloc] init];
////    NSLog(@"%@",[self class]);
////    NSLog(@"%@",[super superclass]);
//
//
//    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(50, 60, 70, 80)];
//    l.textColor = [UIColor redColor];
//    __block int timerun = 10 ;
//    [self.view addSubview:l];
//
//    NSTimer *t = [NSTimer timerWithTimeInterval:1 repeats:true block:^(NSTimer * _Nonnull timer) {
//        timerun -= 1;
//        if (timerun == 0) {
//            timerun = 10;
//        }
//        l.text = [NSString stringWithFormat:@"%d",timerun];
////                NSLog(@"%f",timer.timeInterval);
//
//    }];
//    [[NSRunLoop mainRunLoop] addTimer:t forMode:NSRunLoopCommonModes] ;
//
////    [NSTimer scheduledTimerWithTimeInterval:1 repeats:true block:^(NSTimer * _Nonnull timer) {
////        NSLog(@"%f",timer.timeInterval);
////
////    }];
//
////    [t invalidate];
//
//
//    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 200, XGWidth,60)];
//    textView.text = @"《服务条款》和《隐私政策》"; textView.backgroundColor=UIColor.whiteColor;
//
//    textView.font = [UIFont systemFontOfSize:12];
//    [self.view addSubview:textView];
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//
//    paragraphStyle.lineSpacing= 1;
//
//    NSDictionary*attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSParagraphStyleAttributeName:paragraphStyle};
//
//    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textView.text attributes:attributes];
//    [attributedString addAttribute:NSLinkAttributeName value:@"fuwutiaokuan://"
//    range:[[attributedString string]rangeOfString:@"《服务条款》"]];
//
//     [attributedString addAttribute:NSLinkAttributeName value:@"yisizhengce://"
//    range:[[attributedString string]rangeOfString:@"《隐私政策》"]];
//
//  //设置统一颜色
//    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColor.redColor range:NSMakeRange(0,textView.text.length)];
//
//     textView.attributedText= attributedString;
//
//     //设置被点击字体颜色
////     textView.linkTextAttributes = @{NSForegroundColorAttributeName:UIColor.blueColor};
//     textView.delegate=self;
//     textView.editable=NO;
//     textView.backgroundColor = UIColor.clearColor;
//
//     textView.scrollEnabled=NO;
//
//}
//- (BOOL)textView:(UITextView*)textView shouldInteractWithURL:(NSURL*)URL inRange:(NSRange)characterRange {
//
//        if ([[URL scheme] isEqualToString:@"fuwutiaokuan"]) {
//
//            NSLog(@"富文本点击 服务条款");
//
//        }else if ([[URL scheme] isEqualToString:@"yisizhengce"]) {
//
//            NSLog(@"富文本点击 隐私政策");
//
//        }
//
//        return YES;
//
//    }
//


@end
